package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.modules.substation.entity.SelectItemInfo;
import io.renren.modules.substation.entity.TBaseDictInfoEntity;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.substation.entity.TBusiSysEventInfoEntity;
import io.renren.modules.substation.service.TBusiSysEventInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 系统事件信息表
 *
 * @author lan
 * @email sunlightcs@gmail.com
 * @date 2020-06-18 20:27:14
 */
@RestController
@RequestMapping("substation/tbusisyseventinfo")
public class TBusiSysEventInfoController extends AbstractController {
    @Autowired
    private TBusiSysEventInfoService tBusiSysEventInfoService;
    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tbusisyseventinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        Map<String, Object> data = new HashMap<>();
        PageUtils page = tBusiSysEventInfoService.queryPage(params);
        int count = tBusiSysEventInfoService.getUnReadCount();//获取未读记录数
        data.put("page", page);
        data.put("count", count);
        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{eventId}")
    @RequiresPermissions("substation:tbusisyseventinfo:info")
    public R info(@PathVariable("eventId") Long eventId){
		TBusiSysEventInfoEntity tBusiSysEventInfo = tBusiSysEventInfoService.getById(eventId);
        if (tBusiSysEventInfo.getReadFlag() == null || "".equals(tBusiSysEventInfo.getReadFlag())
                || "0".equals(tBusiSysEventInfo.getReadFlag())){// 查看详情后将阅读标志修改为1-已读
            tBusiSysEventInfo.setReadFlag("1");
            tBusiSysEventInfo.setLastOpr(getUserId());
            tBusiSysEventInfo.setLastTime(new Date());
            tBusiSysEventInfoService.updateById(tBusiSysEventInfo);
        }
        return R.ok().put("tBusiSysEventInfo", tBusiSysEventInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tbusisyseventinfo:save")
    public R save(@RequestBody TBusiSysEventInfoEntity tBusiSysEventInfo){
		tBusiSysEventInfoService.save(tBusiSysEventInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tbusisyseventinfo:update")
    public R update(@RequestBody TBusiSysEventInfoEntity tBusiSysEventInfo){
		tBusiSysEventInfoService.updateById(tBusiSysEventInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tbusisyseventinfo:delete")
    public R delete(@RequestBody Long[] eventIds){
		tBusiSysEventInfoService.removeByIds(Arrays.asList(eventIds));

        return R.ok();
    }

    /**
     * 事件类型列表
     */
    @RequestMapping("/getEventTypeList")
    @RequiresPermissions("substation:tbusisyseventinfo:getEventTypeList")
    public R getEventTypeList(@RequestParam Map<String, Object> params){
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List = tBaseDictInfoService.queryByDictKey("EVENT_TYPE");
        for(int i=0;i<dic_List.size();i++) {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }

        return R.ok().put("list", retList);
    }

}
